/*
 * LPBParser.cpp
 *
 * Copyright 2012 Fabian Wenzelmann, Christian Schilling
 *
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 *
 * Recognition-Procedures-for-Boolean-Functions is free software: you
 * can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.
 *
 * If not, see <http://www.gnu.org/licenses/>.
 */

#include "bfr/LPBParser.hpp"

#include <iostream>
#include <string>

#include <boost/lexical_cast.hpp>

#include "bfr/DIMACSParser.hpp"
#include "bfr/LPB.hpp"
#include "bfr/CoefficientType.hpp"
#include "bfr/Literal.hpp"

using std::string;
using boost::lexical_cast;
using boost::bad_lexical_cast;

namespace bfr {

LPBParser::LPBParser() { }
LPBParser::~LPBParser() { }

size_t LPBParser::parseNumber(std::string &line, size_t *pos) throw(std::string) {
	// search for the next space that terminates the number
	size_t nextSpace = line.find(' ', *pos);
	if (nextSpace == std::string::npos) {
		// there is no " ". So we assume the the rest of the
		// line is the whole number
		nextSpace = line.size();
	}
	// get the substring
	size_t len = nextSpace - *pos;
	std::string substr = line.substr(*pos, len);
	try {
		*pos = nextSpace + 1;
		return boost::lexical_cast<size_t>( substr );
	}
	catch (const boost::bad_lexical_cast &e ) {
		throw std::string("Can't parse ") + substr + " as number.";
	}
}

LPB* LPBParser::parse(std::istream &in) const throw(string) {
	if ((!in.good()) || in.eof()) {
		throw "Can't read from input stream.";
	}
	size_t numVariables;
	string line;
	getline(in, line);
	try {
		numVariables = lexical_cast<size_t>(line);
	}
	catch (const bad_lexical_cast &e) {
		throw string(e.what());
	}
	// parse degree
	lpb_coefficient_t degree;
	if (in.eof()) {
		throw "Can't read degree because end of file is reached.";
	}
	getline(in, line);
	try {
		degree = lexical_cast<lpb_coefficient_t>(line);
	}
	catch (const bad_lexical_cast &e) {
		throw string(e.what());
	}
	LPB *result = new LPB(degree, numVariables);
	// read the coefficients
	for (size_t i = 0; i < numVariables; i++) {
		if (in.eof()) {
			throw "Can't read degree because end of file is reached.";
		}
		getline(in, line);
		try {
			lpb_coefficient_t coeff = lexical_cast<lpb_coefficient_t>(line);
			result->addSummand(coeff, Literal(i));
		}
		catch (const bad_lexical_cast &e) {
			delete result;
			throw string(e.what());
		}
	}
	return result;
}

  std::vector<LPB*> *LPBParser::parse_collection(std::istream &in) throw(string) {
	if ((!in.good()) || in.eof()) {
		throw "Can't read from input stream.";
	}
	size_t pos;
	lpb_coefficient_t coeff;
	size_t degree;
	size_t numVariables;
	string line;
	LPB *lpb;
	std::vector<LPB*> *result = new std::vector<LPB*>();
	while (!(in.eof())) {
	  getline(in, line); 
	  pos = 0;
	  coeff = -1;
	  try {
	    numVariables = parseNumber(line, &pos);
	  }
	  catch (const std::string &str) {
            throw str;
	  }
	  try {
	    degree = parseNumber(line, &pos);
	  }
	  catch (const std::string &str) {
            throw str;
	  }
	  lpb = new LPB(degree, numVariables);
	// read the coefficients
	  for (size_t i = 0; i < numVariables; i++) {
	    try {
	      coeff = parseNumber(line, &pos);
	      if (coeff != 0) { 			
		lpb->addSummand(coeff, Literal(i));
	      }
	    }
	    catch (const std::string &str) {
	      throw str;
	    }
	  }
	  result->push_back(lpb);
	}
	return result;
}
}
